<?php
namespace console\controllers;

use common\models\DomainAuthCode;
use Yii;
use yii\console\Controller;

/**]
 * Class DomainAuthController
 * @package console\controllers
 */
class DomainAuthController extends Controller
{

    public function actionIndex()
    {
        echo 'hello cli';
    }

    /**
     * 生成一堆串
     *  php yii domain-auth/generate-code
     */
    public function actionGenerateCode()
    {
        $alias = 'cwgj';
        $countNumber = 500;
        $insertData = [
            'alias' => $alias,
            'state' => DomainAuthCode::STATE_UNUSED,
        ];
        for ($i=0;$i<$countNumber;$i++) {
            $insertData['code'] = $this->genCode(32);
            $model = new DomainAuthCode();
            $model->load($insertData,'');
            try {
                $model->save();
            }catch (\Exception $e) {
                echo $e->getMessage().PHP_EOL;
            }
        }
        echo '已经生成'.PHP_EOL;
    }

    /**
     * 生成随机串
     * @param $length
     *
     * @return string
     */
    protected function genCode($length)
    {
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str)-1;
        $randStr = '';
        for ($i=0;$i<$length;$i++) {
            $num=mt_rand(0,$len);
            $randStr .= $str[$num];
        }
        return $randStr;
    }

}
